{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 获取数据集\n",
    "# 2. 基本数据处理\n",
    "# 2.1 缩小数据范围\n",
    "# 2.2 选择时间特征\n",
    "# 2.3 去掉签到较少的地方\n",
    "# 2.4 确定特征值和目标值\n",
    "# 2.5 分割数据集\n",
    "# 3. 特征工程--特征预处理（标准化）\n",
    "# 4. 机器学习--knn+cv\n",
    "# 5. 模型评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv('./data/FBlocation/train.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.7941</td>\n",
       "      <td>9.0809</td>\n",
       "      <td>54</td>\n",
       "      <td>470702</td>\n",
       "      <td>8523065625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>5.9567</td>\n",
       "      <td>4.7968</td>\n",
       "      <td>13</td>\n",
       "      <td>186555</td>\n",
       "      <td>1757726713</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>8.3078</td>\n",
       "      <td>7.0407</td>\n",
       "      <td>74</td>\n",
       "      <td>322648</td>\n",
       "      <td>1137537235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>7.3665</td>\n",
       "      <td>2.5165</td>\n",
       "      <td>65</td>\n",
       "      <td>704587</td>\n",
       "      <td>6567393236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>4.0961</td>\n",
       "      <td>1.1307</td>\n",
       "      <td>31</td>\n",
       "      <td>472130</td>\n",
       "      <td>7440663949</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   row_id       x       y  accuracy    time    place_id\n",
       "0       0  0.7941  9.0809        54  470702  8523065625\n",
       "1       1  5.9567  4.7968        13  186555  1757726713\n",
       "2       2  8.3078  7.0407        74  322648  1137537235\n",
       "3       3  7.3665  2.5165        65  704587  6567393236\n",
       "4       4  4.0961  1.1307        31  472130  7440663949"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.455901e+07</td>\n",
       "      <td>4.999770e+00</td>\n",
       "      <td>5.001814e+00</td>\n",
       "      <td>8.284912e+01</td>\n",
       "      <td>4.170104e+05</td>\n",
       "      <td>5.493787e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>8.405649e+06</td>\n",
       "      <td>2.857601e+00</td>\n",
       "      <td>2.887505e+00</td>\n",
       "      <td>1.147518e+02</td>\n",
       "      <td>2.311761e+05</td>\n",
       "      <td>2.611088e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000016e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>7.279505e+06</td>\n",
       "      <td>2.534700e+00</td>\n",
       "      <td>2.496700e+00</td>\n",
       "      <td>2.700000e+01</td>\n",
       "      <td>2.030570e+05</td>\n",
       "      <td>3.222911e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.455901e+07</td>\n",
       "      <td>5.009100e+00</td>\n",
       "      <td>4.988300e+00</td>\n",
       "      <td>6.200000e+01</td>\n",
       "      <td>4.339220e+05</td>\n",
       "      <td>5.518573e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.183852e+07</td>\n",
       "      <td>7.461400e+00</td>\n",
       "      <td>7.510300e+00</td>\n",
       "      <td>7.500000e+01</td>\n",
       "      <td>6.204910e+05</td>\n",
       "      <td>7.764307e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>1.000000e+01</td>\n",
       "      <td>1.000000e+01</td>\n",
       "      <td>1.033000e+03</td>\n",
       "      <td>7.862390e+05</td>\n",
       "      <td>9.999932e+09</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             row_id             x             y      accuracy          time  \\\n",
       "count  2.911802e+07  2.911802e+07  2.911802e+07  2.911802e+07  2.911802e+07   \n",
       "mean   1.455901e+07  4.999770e+00  5.001814e+00  8.284912e+01  4.170104e+05   \n",
       "std    8.405649e+06  2.857601e+00  2.887505e+00  1.147518e+02  2.311761e+05   \n",
       "min    0.000000e+00  0.000000e+00  0.000000e+00  1.000000e+00  1.000000e+00   \n",
       "25%    7.279505e+06  2.534700e+00  2.496700e+00  2.700000e+01  2.030570e+05   \n",
       "50%    1.455901e+07  5.009100e+00  4.988300e+00  6.200000e+01  4.339220e+05   \n",
       "75%    2.183852e+07  7.461400e+00  7.510300e+00  7.500000e+01  6.204910e+05   \n",
       "max    2.911802e+07  1.000000e+01  1.000000e+01  1.033000e+03  7.862390e+05   \n",
       "\n",
       "           place_id  \n",
       "count  2.911802e+07  \n",
       "mean   5.493787e+09  \n",
       "std    2.611088e+09  \n",
       "min    1.000016e+09  \n",
       "25%    3.222911e+09  \n",
       "50%    5.518573e+09  \n",
       "75%    7.764307e+09  \n",
       "max    9.999932e+09  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(29118021, 6)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 基本数据处理\n",
    "# 2.1 缩小数据范围\n",
    "# facebook_data = data.query('x > 2.0 & x < 2.5 & y > 2.0 & y < 2.5')\n",
    "facebook_data = data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.7941</td>\n",
       "      <td>9.0809</td>\n",
       "      <td>54</td>\n",
       "      <td>470702</td>\n",
       "      <td>8523065625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>5.9567</td>\n",
       "      <td>4.7968</td>\n",
       "      <td>13</td>\n",
       "      <td>186555</td>\n",
       "      <td>1757726713</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>8.3078</td>\n",
       "      <td>7.0407</td>\n",
       "      <td>74</td>\n",
       "      <td>322648</td>\n",
       "      <td>1137537235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>7.3665</td>\n",
       "      <td>2.5165</td>\n",
       "      <td>65</td>\n",
       "      <td>704587</td>\n",
       "      <td>6567393236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>4.0961</td>\n",
       "      <td>1.1307</td>\n",
       "      <td>31</td>\n",
       "      <td>472130</td>\n",
       "      <td>7440663949</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   row_id       x       y  accuracy    time    place_id\n",
       "0       0  0.7941  9.0809        54  470702  8523065625\n",
       "1       1  5.9567  4.7968        13  186555  1757726713\n",
       "2       2  8.3078  7.0407        74  322648  1137537235\n",
       "3       3  7.3665  2.5165        65  704587  6567393236\n",
       "4       4  4.0961  1.1307        31  472130  7440663949"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "facebook_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(29118021, 6)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "facebook_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    470702\n",
       "1    186555\n",
       "2    322648\n",
       "3    704587\n",
       "4    472130\n",
       "Name: time, dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.2 选择时间特征\n",
    "facebook_data['time'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   1970-01-06 10:45:02\n",
       "1   1970-01-03 03:49:15\n",
       "2   1970-01-04 17:37:28\n",
       "3   1970-01-09 03:43:07\n",
       "4   1970-01-06 11:08:50\n",
       "Name: time, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time = pd.to_datetime(facebook_data['time'], unit='s')\n",
    "# 脱敏\n",
    "time.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "time = pd.DatetimeIndex(time)\n",
    "# time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "facebook_data['hour'] = time.hour\n",
    "# time.hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "facebook_data['day'] = time.day\n",
    "# time.day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "facebook_data['weekday'] = time.weekday\n",
    "# time.weekday"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "      <th>hour</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.7941</td>\n",
       "      <td>9.0809</td>\n",
       "      <td>54</td>\n",
       "      <td>470702</td>\n",
       "      <td>8523065625</td>\n",
       "      <td>10</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>5.9567</td>\n",
       "      <td>4.7968</td>\n",
       "      <td>13</td>\n",
       "      <td>186555</td>\n",
       "      <td>1757726713</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>8.3078</td>\n",
       "      <td>7.0407</td>\n",
       "      <td>74</td>\n",
       "      <td>322648</td>\n",
       "      <td>1137537235</td>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>7.3665</td>\n",
       "      <td>2.5165</td>\n",
       "      <td>65</td>\n",
       "      <td>704587</td>\n",
       "      <td>6567393236</td>\n",
       "      <td>3</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>4.0961</td>\n",
       "      <td>1.1307</td>\n",
       "      <td>31</td>\n",
       "      <td>472130</td>\n",
       "      <td>7440663949</td>\n",
       "      <td>11</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   row_id       x       y  accuracy    time    place_id  hour  day  weekday\n",
       "0       0  0.7941  9.0809        54  470702  8523065625    10    6        1\n",
       "1       1  5.9567  4.7968        13  186555  1757726713     3    3        5\n",
       "2       2  8.3078  7.0407        74  322648  1137537235    17    4        6\n",
       "3       3  7.3665  2.5165        65  704587  6567393236     3    9        4\n",
       "4       4  4.0961  1.1307        31  472130  7440663949    11    6        1"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "facebook_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.3 去掉签到较少的地方\n",
    "place_count = facebook_data.groupby('place_id').count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>hour</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>place_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1000015801</th>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1000017288</th>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1000025138</th>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1000052096</th>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1000063498</th>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            row_id    x    y  accuracy  time  hour  day  weekday\n",
       "place_id                                                        \n",
       "1000015801      78   78   78        78    78    78   78       78\n",
       "1000017288      95   95   95        95    95    95   95       95\n",
       "1000025138     563  563  563       563   563   563  563      563\n",
       "1000052096     961  961  961       961   961   961  961      961\n",
       "1000063498      60   60   60        60    60    60   60       60"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "place_count.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>hour</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>place_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1000015801</th>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1000017288</th>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1000025138</th>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "      <td>563</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1000052096</th>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "      <td>961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1000063498</th>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            row_id    x    y  accuracy  time  hour  day  weekday\n",
       "place_id                                                        \n",
       "1000015801      78   78   78        78    78    78   78       78\n",
       "1000017288      95   95   95        95    95    95   95       95\n",
       "1000025138     563  563  563       563   563   563  563      563\n",
       "1000052096     961  961  961       961   961   961  961      961\n",
       "1000063498      60   60   60        60    60    60   60       60"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "place_count = place_count[place_count['row_id'] > 3]\n",
    "place_count.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([1000015801, 1000017288, 1000025138, 1000052096, 1000063498,\n",
       "            1000213704, 1000383269, 1000392527, 1000472949, 1000474694,\n",
       "            ...\n",
       "            9999431769, 9999450678, 9999515258, 9999639970, 9999755282,\n",
       "            9999851158, 9999855083, 9999862567, 9999916757, 9999932225],\n",
       "           dtype='int64', name='place_id', length=107814)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "place_count.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "      <th>hour</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.7941</td>\n",
       "      <td>9.0809</td>\n",
       "      <td>54</td>\n",
       "      <td>470702</td>\n",
       "      <td>8523065625</td>\n",
       "      <td>10</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>5.9567</td>\n",
       "      <td>4.7968</td>\n",
       "      <td>13</td>\n",
       "      <td>186555</td>\n",
       "      <td>1757726713</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>8.3078</td>\n",
       "      <td>7.0407</td>\n",
       "      <td>74</td>\n",
       "      <td>322648</td>\n",
       "      <td>1137537235</td>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>7.3665</td>\n",
       "      <td>2.5165</td>\n",
       "      <td>65</td>\n",
       "      <td>704587</td>\n",
       "      <td>6567393236</td>\n",
       "      <td>3</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>4.0961</td>\n",
       "      <td>1.1307</td>\n",
       "      <td>31</td>\n",
       "      <td>472130</td>\n",
       "      <td>7440663949</td>\n",
       "      <td>11</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   row_id       x       y  accuracy    time    place_id  hour  day  weekday\n",
       "0       0  0.7941  9.0809        54  470702  8523065625    10    6        1\n",
       "1       1  5.9567  4.7968        13  186555  1757726713     3    3        5\n",
       "2       2  8.3078  7.0407        74  322648  1137537235    17    4        6\n",
       "3       3  7.3665  2.5165        65  704587  6567393236     3    9        4\n",
       "4       4  4.0961  1.1307        31  472130  7440663949    11    6        1"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "facebook_data = facebook_data[facebook_data['place_id'].isin(place_count.index)]\n",
    "facebook_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(29116952, 9)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "facebook_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.4 确定特征值和目标值\n",
    "x = facebook_data[['x', 'y', 'accuracy', 'day', 'hour', 'weekday']]\n",
    "y = facebook_data['place_id']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.5 分割数据集\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=2, test_size=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 特征工程--特征预处理（标准化）\n",
    "# 3.1 实例化一个转换器\n",
    "transfer = StandardScaler()\n",
    "\n",
    "# 3.2 调用fit_transform\n",
    "x_train = transfer.fit_transform(x_train)\n",
    "x_test = transfer.transform(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\think\\envs\\ai\\lib\\site-packages\\sklearn\\model_selection\\_split.py:605: Warning: The least populated class in y has only 1 members, which is too few. The minimum number of members in any class cannot be less than n_splits=9.\n",
      "  % (min_groups, self.n_splits)), Warning)\n"
     ]
    }
   ],
   "source": [
    "# 4. 机器学习--knn+cv\n",
    "# 4.1 实例化一个估计器\n",
    "estimator = KNeighborsClassifier()\n",
    "\n",
    "# 4.2 调用交叉验证网格搜索\n",
    "param_grid = {'n_neighbors': [3, 5, 7]}\n",
    "estimator = GridSearchCV(estimator=estimator, param_grid=param_grid, cv=9, n_jobs=14)\n",
    "\n",
    "# 4.3 训练\n",
    "estimator.fit(x_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测值为:\n",
      " [2225211839 8980163153 1247398579 ... 3610237287 7727090889 2615125270]\n",
      "准确率:\n",
      " 0.3772468057460478\n"
     ]
    }
   ],
   "source": [
    "# 5. 模型评估\n",
    "# 5.1 预测值输出\n",
    "y_pre = estimator.predict(x_test)\n",
    "print('预测值为:\\n', y_pre)\n",
    "\n",
    "# 5.2 score\n",
    "score = estimator.score(x_test, y_test)\n",
    "print('准确率:\\n', score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最好的模型:\n",
      " KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
      "           metric_params=None, n_jobs=1, n_neighbors=5, p=2,\n",
      "           weights='uniform')\n",
      "最好的结果:\n",
      " 0.36527043366840517\n",
      "整体模型结果:\n",
      " {'mean_fit_time': array([0.12965936, 0.24237047, 0.32704976]), 'std_fit_time': array([0.0109939 , 0.09217544, 0.11499942]), 'mean_score_time': array([0.46159214, 1.04580153, 1.85831748]), 'std_score_time': array([0.08695226, 0.49836622, 0.46353756]), 'param_n_neighbors': masked_array(data=[3, 5, 7],\n",
      "             mask=[False, False, False],\n",
      "       fill_value='?',\n",
      "            dtype=object), 'params': [{'n_neighbors': 3}, {'n_neighbors': 5}, {'n_neighbors': 7}], 'split0_test_score': array([0.342718  , 0.35017501, 0.34850099]), 'split1_test_score': array([0.3350246 , 0.35424354, 0.3499385 ]), 'split2_test_score': array([0.34802929, 0.35176819, 0.34834086]), 'split3_test_score': array([0.3446795 , 0.36122157, 0.35629076]), 'split4_test_score': array([0.35210809, 0.37180531, 0.36578219]), 'split5_test_score': array([0.36419446, 0.3703335 , 0.37464742]), 'split6_test_score': array([0.36081081, 0.37263514, 0.37331081]), 'split7_test_score': array([0.35783133, 0.3777969 , 0.37986231]), 'split8_test_score': array([0.37172775, 0.38202443, 0.38010471]), 'mean_test_score': array([0.35254733, 0.36527043, 0.36350183]), 'std_test_score': array([0.01095118, 0.01109373, 0.01271785]), 'rank_test_score': array([3, 1, 2]), 'split0_train_score': array([0.61461916, 0.55100328, 0.51322686]), 'split1_train_score': array([0.6149222 , 0.55184329, 0.514364  ]), 'split2_train_score': array([0.61614141, 0.55282495, 0.51418599]), 'split3_train_score': array([0.6145265 , 0.55182803, 0.51429037]), 'split4_train_score': array([0.61376553, 0.55076317, 0.51305107]), 'split5_train_score': array([0.61325531, 0.54857849, 0.51091447]), 'split6_train_score': array([0.61122224, 0.54904932, 0.51102221]), 'split7_train_score': array([0.61158041, 0.54750912, 0.50946553]), 'split8_train_score': array([0.61162215, 0.54735211, 0.51033594]), 'mean_train_score': array([0.61351721, 0.55008353, 0.51231738]), 'std_train_score': array([0.0016273 , 0.00189696, 0.00178398])}\n"
     ]
    }
   ],
   "source": [
    "# 5.3 其他评价指标\n",
    "print('最好的模型:\\n', estimator.best_estimator_)\n",
    "print('最好的结果:\\n', estimator.best_score_)\n",
    "print('整体模型结果:\\n', estimator.cv_results_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
